package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.duer.smartmate.protocol.dlp.bean.screen.ImageListCardPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.ListCardPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.RenderPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.RenderType;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.RenderVoiceInputTextPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.StandardCardPayload;
import com.baidu.duer.smartmate.protocol.dlp.bean.screen.TextCardPayload;
import com.baidu.duer.smartmate.proxy.bean.RenderPlayerMessage;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;

/**
 * Created by niufei01 on 2017/10/20.
 */

public class SpeechAndCardDemo extends BaseFragment {

    TextView textView;
    ControllerManager controllerManager;

    @Override
    public View onCreateContentViewCompat(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.speech_card, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        textView = (TextView) view.findViewById(R.id.text_view);
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerRenderObserver(observer);
    }

    DCSDataObserver<RenderPayload> observer = new DCSDataObserver<RenderPayload>() {
        @Override
        public void onDataChanaged(String name, RenderPayload payload) {
            // 参考screen.md, screen-private.md, screen-extended-card.md中的说明
            if (payload.getRenderType() == RenderType.VOICE_INPUT_TEXT) {
                // 显示用户的query识别结果
                RenderVoiceInputTextPayload renderVoiceInputTextPayload = (RenderVoiceInputTextPayload) payload;
                textView.append("\n");
                textView.append("query::(" +((RenderVoiceInputTextPayload) payload).getType()+ ")");
                textView.append(renderVoiceInputTextPayload.getText());
            } else if (payload.getRenderType() == RenderType.TEXT_CARD) {
                TextCardPayload textCardPayload = (TextCardPayload) payload;
                formatPrint(textCardPayload.getContent());
            } else if (payload.getRenderType() == RenderType.STANDARD_CARD) {
                StandardCardPayload standardCardPayload = (StandardCardPayload) payload;
                formatPrint(standardCardPayload.getContent());
            } else if (payload.getRenderType() == RenderType.LIST_CARD) {
                ListCardPayload listCardPayload = (ListCardPayload) payload;
                formatPrint(listCardPayload.getTitle());
            } else if (payload.getRenderType() == RenderType.IMAGE_LIST_CARD) {
                ImageListCardPayload imageListCardPayload = (ImageListCardPayload) payload;
                formatPrint(imageListCardPayload.getJson());
            } else if (payload.getRenderType() == RenderType.RENDER_PLAYER_INFO) {
                RenderPlayerMessage renderPlayerMessage = (RenderPlayerMessage) payload;
                formatPrint(renderPlayerMessage.getContent().getTitle());
            } else {
                formatPrint(payload.getJson());
            }
        }
    };

    private void formatPrint(String content) {
        if (content == null) {
            return;
        }
        textView.append("\n");
        textView.append("response:");
        textView.append("\n");
        textView.append(content);
        textView.append("\n");
        textView.append("---------------------------------------------------------------------");
    }

    @Override
    public void onDestroy() {
        if (controllerManager != null) {
            controllerManager.unregisterRenderObserver(observer);
        }
        super.onDestroy();
    }
}
